package DesInterface;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;

import cryptage.Cryptage;

/**
 * Creation de l'interface graphique
 * @author marie, margaux
 */
public class DesPanel extends JFrame {
    
	private static final long serialVersionUID = 1L;
    
    private Cryptage encryption;
    
    private	Container c = this.getContentPane();
    
    private JPanel leftPanel = new JPanel();
    private JPanel rightPanel = new JPanel();
    
    private JLabel keyLabel = new JLabel("Clef : ");
    private JPanel keyPanel = new JPanel();
    private JPanel originalPanel = new JPanel();
    private JPanel cipherPanel = new JPanel();
    private JPanel decipherPanel = new JPanel();
    private JPanel stepsPanel = new JPanel();   
     
    private JButton cipherButton = new JButton("Chiffrer");
    private JButton decipherButton = new JButton("Dechiffrer");
    
    private JTextField keyText = new JTextField(16);
    private JTextArea originalText = new JTextArea(10, 45);
    private JTextArea cipherText = new JTextArea(5, 45);
    private JTextArea decipherText = new JTextArea(10, 45);
    public static JTextArea stepsText = new JTextArea(40, 45);
    
    private JScrollPane originalScroll = new JScrollPane(originalText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    		                                                                           JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JScrollPane cipherScroll = new JScrollPane(cipherText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    																				   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JScrollPane decipherScroll = new JScrollPane(decipherText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    																				   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JScrollPane stepsScroll = new JScrollPane(stepsText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    																				   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      
    
    /**
     * Constructeur : initialisation de la fenetre principale
     */
    public DesPanel() {
        this.setTitle("Projet DES");
        this.setSize(1260,740);
        this.setVisible(true);
        
        GenerateGUI();
    }
    
    /**
     * Creation de la fenetres et de son contenu
     */
    private void GenerateGUI() {
        c.setLayout(new GridLayout(1,2));
        
        Document doc = keyText.getDocument();
        AbstractDocument absDoc = (AbstractDocument )doc;
        absDoc.setDocumentFilter(new DocumentSizeFilter(8));
        keyText.setSize(10,1);
        
        keyPanel.setLayout(new FlowLayout());
        keyPanel.add(keyLabel);
        keyPanel.add(keyText);        
        
        originalPanel.setBorder(BorderFactory.createTitledBorder("Texte original"));
        originalPanel.setLayout(new FlowLayout());
        originalPanel.add(originalScroll);
        
        cipherPanel.setBorder(BorderFactory.createTitledBorder("Texte chiffre"));
        cipherPanel.setLayout(new FlowLayout());
        cipherPanel.add(cipherScroll);
        
        decipherPanel.setBorder(BorderFactory.createTitledBorder("Texte dechiffre"));
        decipherPanel.setLayout(new FlowLayout());
        decipherPanel.add(decipherScroll);
        
        stepsPanel.setBorder(BorderFactory.createTitledBorder("Moniteur"));
        stepsPanel.setLayout(new FlowLayout());
        stepsPanel.add(stepsScroll);        
        
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        leftPanel.add(keyPanel);
        leftPanel.add(originalPanel);
        leftPanel.add(cipherButton);
        leftPanel.add(cipherPanel);
        leftPanel.add(decipherButton);
        leftPanel.add(decipherPanel);
        
        rightPanel.setLayout(new FlowLayout());
        rightPanel.add(stepsPanel);
        
        c.add(leftPanel);
        c.add(rightPanel);
        
        DoActions();   
    }
    
    /**
     * Actions a faire : listener
     */
    private void DoActions() {
        ActionHandler action = new ActionHandler();        
        cipherButton.addActionListener(action);
        decipherButton.addActionListener(action);
    }
    
    
    /**
     * ActionHandler implementant ActionListener
     * @author marie, margaux
     */
    private class ActionHandler implements ActionListener {        
        
    	/**
    	 * Methode permettant l'action souhaitee
    	 */
        public void actionPerformed(ActionEvent e) {
        	
        	// si il y a eu un clic sur le bouton de chiffrement
            if (e.getSource() == cipherButton) {
            	
            	// recuperation du texte en clair
                String plaintext = originalText.getText();
                
                // recuperation de la cle
                String keyworD = keyText.getText();
                
                // on affiche les donnees dans les etapes 
                stepsText.append("clef : " + keyworD + "\n");
                stepsText.append("texte chiffre : " + plaintext + "\n");
                
                // creation d'un type encrypt afin de chiffrer
                encryption = new Cryptage(plaintext,keyworD);
                
                // on chiffre
                encryption.doEncryption();
                
                // on met a jour le texte chiffre
                cipherText.append(encryption.getEncryption());
            }
            
            //si il y a eu un clic sur le bouton de dechiffrement
            if (e.getSource() == decipherButton) {
            	
            	// on decrypte
                encryption.doDecryption();
                
                // on met a jour le texte dechiffre
                //decipherText.append(encryption.getBinDecipher() + "\n");
                decipherText.setCaretPosition(decipherText.getDocument().getLength());
                decipherText.append(encryption.getDecryption());
            }
        }    
    } 
    
    /**
     * Methode permettant d'afficher un tableau associe a son etiquette (label)
     * @param array le tableau 
     * @param label l'etiquette
     */
    public static void printArray(String[][] array, String label) {
    	
        stepsText.append("-- " + label + " -- " + '\n');
        
        for(int i = 0; i < array.length; i++) {
        	stepsText.append("| ");
            
        	for(int j = 0; j < array[0].length; j++) {
        		stepsText.append(array[i][j] + " ");
            }
        	
            stepsText.append("| " + '\n');
        }
    }
}